package com.lhz.project.system.controller.base;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.lhz.common.page.ResponseObject;
import com.lhz.framework.aspect.log.annotation.SysLog;
import com.lhz.framework.security.annotation.IgnoreToken;
import com.lhz.project.system.model.vo.base.FileUploadVo;
import com.lhz.project.system.service.base.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.List;

@Tag(name = "文件管理")
@RestController
@RequestMapping("/file")
@ApiSort(2)
public class FileUploadController {
    @Resource
    private FileUploadService fileUploadService;

    /**
     * 单文件上传
     */
    @Operation(summary = "文件上传(可多选)", description = "文件上传(可多选)")
    @ApiOperationSupport(order = 10)
    @Parameters({
            @Parameter(name = "dataId", description = "业务Id", in = ParameterIn.QUERY, required = true, schema = @Schema(type = "String")),
            @Parameter(name = "files", description = "文件", required = true, schema = @Schema(name = "files", format = "binary")),
    })
    @PostMapping(value = "/upload")
    @SysLog(model = "文件管理", name = "文件上传")
    public ResponseObject uploadFile(@RequestParam("dataId") String dataId, @RequestParam("files") List<MultipartFile> files) {

        Object obj = fileUploadService.uploadFile(dataId, files);

        return ResponseObject.success(obj);
    }

    /**
     * 删除单个文件
     */
    @Operation(summary = "删除单个文件", description = "删除单个文件")
    @ApiOperationSupport(order = 20)
    @Parameters({@Parameter(name = "fileId", description = "文件Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),
    })
    @SysLog(model = "文件管理", name = "删除单个文件")
    @DeleteMapping(value = "/delete/file/{fileId}")
    public ResponseObject deleteUploadFile(@PathVariable("fileId") String fileId) {

        fileUploadService.deleteUploadFile(fileId);

        return ResponseObject.successDel();
    }

    /**
     * 删除业务文件
     */
    @Operation(summary = "删除业务文件", description = "删除业务文件")
    @ApiOperationSupport(order = 25)
    @Parameters({@Parameter(name = "dataId", description = "业务Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),
    })
    @SysLog(model = "文件管理", name = "删除业务文件")
    @DeleteMapping(value = "/delete/data/{dataId}")
    public ResponseObject deleteUploadFileByData(@PathVariable("dataId") String dataId) {

        fileUploadService.deleteUploadFileByData(dataId);

        return ResponseObject.successDel();
    }

    /**
     * 获取业务文件列表
     */
    @Operation(summary = "获取业务文件列表", description = "获取业务文件列表")
    @ApiOperationSupport(order = 30)
    @Parameters({@Parameter(name = "dataId", description = "业务Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),
    })
    @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = FileUploadVo.class))))
    @SysLog(model = "文件管理", name = "获取业务文件列表")
    @GetMapping(value = "/file/list/{dataId}")
    public ResponseObject getFileListByData(@PathVariable("dataId") String dataId) {

        Object obj = fileUploadService.getFileListByData(dataId);

        return ResponseObject.success(obj);
    }

    /**
     * 下载单个文件
     */
    @IgnoreToken
    @Operation(summary = "下载单个文件(文件流)", description = "下载单个文件(文件流)")
    @ApiOperationSupport(order = 35)
    @Parameters({@Parameter(name = "fileId", description = "文件Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),})
    @SysLog(model = "文件管理", name = "下载单个文件")
    @GetMapping(value = "/down/file/stream/{fileId}")
    public void downloadFileStream(@PathVariable("fileId") String fileId) {
        fileUploadService.downloadFileStream(fileId);
    }

    /**
     * 批量下载业务文件
     */
    @IgnoreToken
    @Operation(summary = "批量下载业务文件(ZIP文件流)", description = "批量下载业务文件(ZIP文件流)")
    @ApiOperationSupport(order = 40)
    @Parameters({@Parameter(name = "dataId", description = "业务Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),})
    @SysLog(model = "文件管理", name = "批量下载业务文件")
    @GetMapping(value = "/down/data/stream/{dataId}")
    public void downloadFileByDataStream(@PathVariable("dataId") String dataId) {
        LocalDate now = LocalDate.now();
        String s = now.toString();
        fileUploadService.downloadFileByDataStream(dataId);
    }

    /**
     * 下载单个文件
     */
    @Operation(summary = "下载单个文件(文件地址)", description = "下载单个文件(文件地址)")
    @ApiOperationSupport(order = 45)
    @Parameters({@Parameter(name = "fileId", description = "文件Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),})
    @SysLog(model = "文件管理", name = "下载单个文件")
    @GetMapping(value = "/down/file/{fileId}")
    public ResponseObject downloadFile(@PathVariable("fileId") String fileId) {
        Object o = fileUploadService.downloadFile(fileId);
        return ResponseObject.success(o);
    }

    /**
     * 批量下载业务文件
     */
    @Operation(summary = "批量下载业务文件(ZIP文件地址)", description = "批量下载业务文件(ZIP文件地址)")
    @ApiOperationSupport(order = 50)
    @Parameters({@Parameter(name = "dataId", description = "业务Id", in = ParameterIn.PATH, schema = @Schema(type = "String"), required = true),})
    @SysLog(model = "文件管理", name = "批量下载业务文件")
    @GetMapping(value = "/down/data/{dataId}")
    public ResponseObject downloadFileByData(@PathVariable("dataId") String dataId) {
        Object o = fileUploadService.downloadFileByData(dataId);
        return ResponseObject.success(o);
    }
}
